<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\HouseBrokers;
use App\Admin\Repositories\HouseBroker;
use App\Admin\Repositories\HouseList;
use App\Models\AddressModel;
use App\Models\HouseTypeInfo;
use App\Services\ElasticSearchService;
use App\Services\RedisGeo;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HouseListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HouseList(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('house_logo','图片')->image();
            $grid->column('house_name','小区名称');
            $grid->column('house_type','房源类型')->display(function ($house_type){

                $name= HouseTypeInfo::where('id',$house_type)->first();

                return $name['house_type_name'];
            });;
            $grid->column('house_area','区域');
            $grid->column('house_moldings','房源户型');
            $grid->column('house_floor','楼层');
            $grid->column('house_acreage','房源面积');
            $grid->column('house_price','售价');
            $grid->column('house_title','房源标题');
            $grid->column('house_text','简介');
            $grid->column('status','审核状态')->radio([0=>'关闭',1=>'进行中']);
            $grid->column('square_price','平方价格');
            $grid->column('house_way','入住方式')->radio([1=>'买房',2=>'租房']);
            $grid->column('house_address','房源地址');
            $grid->column('house_brokers','经纪人')->display(function ($function){
                $brokerInfo=\App\Models\HouseBroker::where('id',$function)->first();
                if($brokerInfo){
                    return $brokerInfo['broker_name'];
                }else{
                    return '';
                }

            });
            $grid->column('uid','用户id');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HouseList(), function (Show $show) {
            $show->field('id');
            $show->field('house_logo','图片')->image();
            $show->field('house_name','小区名称');
            $show->field('house_type','房源类型');
            $show->field('house_area','区域');
            $show->field('house_moldings','房源户型');
            $show->field('house_floor','楼层');
            $show->field('house_acreage','房源面积');
            $show->field('house_price','售价');
            $show->field('house_title','房源标题');
            $show->field('house_text','简介');
            $show->field('status','审核状态')->using([0=>'关闭',1=>'进行中']);
            $show->field('square_price','平方价格');
            $show->field('house_way','入住方式')->using([1=>'买房',2=>'租房']);
            $show->field('house_address','房源地址');
            $show->field('house_broker','经纪人');
            $show->field('uid','用户id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HouseList(), function (Form $form) {
            $form->display('id');
            $form->image('house_logo','图片')->autoUpload();
            $form->text('house_name','小区名称');
            $form->text('house_type','房源类型');
            $form->text('house_area','区域');
            $form->text('house_moldings','房源户型');
            $form->text('house_floor','楼层');
            $form->text('house_acreage','房源面积');
            $form->text('house_price','售价');
            $form->text('house_title','房源标题');
            $form->text('house_text','简介');
            $form->radio('status','审核状态')->options([0=>'关闭',1=>'进行中']);
            $form->text('square_price','平方价格');
            $form->text('house_way','入住方式')->options([1=>'买房',2=>'租房']);
            $form->text('house_address','房源地址');
            $form->multipleSelectTable('house_brokers','绑定经纪人')->from(HouseBrokers::make())->saving(function ($e){
                $e=implode(',',$e);
                return $e;
            });
            $form->text('uid','用户id');
            $form->display('created_at');
            $form->display('updated_at');

//            $form->select('province','省级')->options('province')->load('city','city');
//            $form->select('city','市级')->load('town','town');
//            $form->select('town','区级');
//
            $form->saving(function (Form $form){
               if ($form->isEditing()){
                   if($form->house_way==1){
                       (new RedisGeo())->setHouseLocation($this->house_city,$this->house_area,$this->house_address,$this->id,'buy');

                   }else{
                       (new RedisGeo())->setHouseLocation($this->house_city,$this->house_area,$this->house_address,$this->id,'rent');
                   }
//                   $res=ElasticSearchService::getInstance();
//                   $res->createDocument('house_list',$this->id,[
//                       'id'=>$this->id,
//                       'house_logo'=>$this->house_logo,
//                       'house_name'=>$this->house_name,
//                       'house_type'=>$this->house_type,
//                       'house_city'=>$this->house_city,
//                       'house_area'=>$this->house_area,
//                       'house_moldings'=>$this->house_moldings,
//                       'house_floor'=>$this->house_floor,
//                       'house_acreage'=>$this->house_acreage,
//                       'house_price'=>$this->house_price,
//                       'house_title'=>$this->house_title,
//                       'house_text'=>$this->house_text,
//                       'square_price'=>$this->square_price,
//                       'house_way'=>$this->house_way,
//                       'status'=>$this->status,
//                       'house_address'=>$this->house_address,
//                       'house_number'=>$this->house_number,
//                       'uid'=>$this->uid,
//                       'latitude'=>$this->latitude,
//                       'longitude'=>$this->longitude
//                   ]);

               }
            });
//
//            $form->submitted(function (Form $form) {
//                // 获取用户提交参数
//                $province = $form->province;
//                $city = $form->city;
//                $town = $form->town;
//                // 上面写法等同于
//                // 删除用户提交的数据
//                $form->deleteInput('province');
//                $form->deleteInput('city');
//                $form->deleteInput('town');
//                if ($province&&$city&&$town){
//                    $address=new AddressModel();
//                    $province=$address->where('id',$province)->select('name')->get()->toarray();
//                    $province=$province[0]['name'];
//                    $city=$address->where('id',$city)->select('name')->get()->toarray()[0]['name'];
//
//                    $town=$address->where('id',$town)->select('name')->get()->toarray()[0]['name'];
////                    $form->house_area=$province.$city.$town;
//                    $form->house_area=$city;
//                }
//            });
        });
    }
}
